package com.ymatou.iapi.godmch.testcase;

import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.GetRolePermissionListBean;
import com.ymatou.iapi.godmch.service.GetRolePermissionListCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.permission;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
/**
*获取角色权限列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetRolePermissionList{
	private static GetRolePermissionListBean getrolepermissionlistBean;
	private static GetRolePermissionListCall getrolepermissionlistCall;
	DefGodMchWapper DefGodMchWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取角色权限列表");
	}

	@Before
	public void caseUp() {
		getrolepermissionlistBean=new GetRolePermissionListBean();
		getrolepermissionlistCall=new GetRolePermissionListCall();
		DefGodMchWapper=new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getrolepermissionlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRolePermissionList_001() {
		Logger.start(true,"获取角色权限列表,成功-销售专员");
		try {
			Integer templateId=1;
			getrolepermissionlistBean.setRoleIds(Arrays.asList(templateId));
			getrolepermissionlistCall.setData(getrolepermissionlistBean);
			getrolepermissionlistCall.callService();			
			Logger.verifyEquals("200", getrolepermissionlistCall.getString("status"), "验证status");
			 List<permission> selectPermission = DefGodMchWapper.selectPermission(templateId);
			 getrolepermissionlistCall.getPermissionDtos().forEach(pd->{
				permission p = selectPermission.stream().filter(sp->{return sp.getPermissionCode().equals(pd.getPermissionCode());}).findFirst().get();
				Logger.verifyEquals(p.getPermissionCode(), pd.getPermissionCode(), "验证PermissionCode");
				Logger.verifyEquals(p.getRoleId(), pd.getRoleId(), "验证RoleId");
				String name="";
				try {
					name = DefGodMchWapper.selectPermissionitemByPermissionCode(pd.getPermissionCode()).get(0).getPermissionName();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Logger.verifyEquals(name, pd.getPermissionName(), "验证getPermissionName");
			 });
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRolePermissionList_002() {
		Logger.start(true,"获取角色权限列表,成功-发货专员");
		try {
			Integer templateId=2;
			getrolepermissionlistBean.setRoleIds(Arrays.asList(templateId));
			getrolepermissionlistCall.setData(getrolepermissionlistBean);
			getrolepermissionlistCall.callService();			
			Logger.verifyEquals("200", getrolepermissionlistCall.getString("status"), "验证status");
			 List<permission> selectPermission = DefGodMchWapper.selectPermission(templateId);
			 getrolepermissionlistCall.getPermissionDtos().forEach(pd->{
				permission p = selectPermission.stream().filter(sp->{return sp.getPermissionCode().equals(pd.getPermissionCode());}).findFirst().get();
				Logger.verifyEquals(p.getPermissionCode(), pd.getPermissionCode(), "验证PermissionCode");
				Logger.verifyEquals(p.getRoleId(), pd.getRoleId(), "验证RoleId");
				String name="";
				try {
					name = DefGodMchWapper.selectPermissionitemByPermissionCode(pd.getPermissionCode()).get(0).getPermissionName();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Logger.verifyEquals(name, pd.getPermissionName(), "验证getPermissionName");
			 });
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRolePermissionList_003() {
		Logger.start(true,"获取角色权限列表,成功-普通运营");
		try {
			Integer templateId=3;
			getrolepermissionlistBean.setRoleIds(Arrays.asList(templateId));
			getrolepermissionlistCall.setData(getrolepermissionlistBean);
			getrolepermissionlistCall.callService();			
			Logger.verifyEquals("200", getrolepermissionlistCall.getString("status"), "验证status");
			 List<permission> selectPermission = DefGodMchWapper.selectPermission(templateId);
			 getrolepermissionlistCall.getPermissionDtos().forEach(pd->{
				permission p = selectPermission.stream().filter(sp->{return sp.getPermissionCode().equals(pd.getPermissionCode());}).findFirst().get();
				Logger.verifyEquals(p.getPermissionCode(), pd.getPermissionCode(), "验证PermissionCode");
				Logger.verifyEquals(p.getRoleId(), pd.getRoleId(), "验证RoleId");
				String name="";
				try {
					name = DefGodMchWapper.selectPermissionitemByPermissionCode(pd.getPermissionCode()).get(0).getPermissionName();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Logger.verifyEquals(name, pd.getPermissionName(), "验证getPermissionName");
			 });
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRolePermissionList_004() {
		Logger.start(true,"获取角色权限列表,成功-高级运营");
		try {
			Integer templateId=4;
			getrolepermissionlistBean.setRoleIds(Arrays.asList(templateId));
			getrolepermissionlistCall.setData(getrolepermissionlistBean);
			getrolepermissionlistCall.callService();			
			Logger.verifyEquals("200", getrolepermissionlistCall.getString("status"), "验证status");
			 List<permission> selectPermission = DefGodMchWapper.selectPermission(templateId);
			 getrolepermissionlistCall.getPermissionDtos().forEach(pd->{
				permission p = selectPermission.stream().filter(sp->{return sp.getPermissionCode().equals(pd.getPermissionCode());}).findFirst().get();
				Logger.verifyEquals(p.getPermissionCode(), pd.getPermissionCode(), "验证PermissionCode");
				Logger.verifyEquals(p.getRoleId(), pd.getRoleId(), "验证RoleId");
				String name="";
				try {
					name = DefGodMchWapper.selectPermissionitemByPermissionCode(pd.getPermissionCode()).get(0).getPermissionName();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Logger.verifyEquals(name, pd.getPermissionName(), "验证getPermissionName");
			 });
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRolePermissionList_005() {
		Logger.start(true,"获取多个角色权限列表,成功");
		try {
			//默认权限 def 1234
			Integer templateId=0;
			getrolepermissionlistBean.setRoleIds(GodmchManageCallService.ALLRolesList);
			getrolepermissionlistCall.setData(getrolepermissionlistBean);
			getrolepermissionlistCall.callService();			
			Logger.verifyEquals("200", getrolepermissionlistCall.getString("status"), "验证status");
			 List<permission> selectPermission = DefGodMchWapper.selectPermission(templateId);
			 getrolepermissionlistCall.getPermissionDtos().forEach(pd->{
				permission p = selectPermission.stream().filter(sp->{return sp.getPermissionCode().equals(pd.getPermissionCode())&&sp.getRoleId().equals(pd.getRoleId());}).findFirst().get();
				Logger.verifyEquals(p.getPermissionCode(), pd.getPermissionCode(), "验证PermissionCode");
				Logger.verifyEquals(p.getRoleId(), pd.getRoleId(), "验证RoleId");
				String name="";
				try {
					name = DefGodMchWapper.selectPermissionitemByPermissionCode(pd.getPermissionCode()).get(0).getPermissionName();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Logger.verifyEquals(name, pd.getPermissionName(), "验证getPermissionName");
			 });
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRolePermissionList_006() {
		Logger.start(true,"不输入roleIds失败");
		try {
			getrolepermissionlistCall.setData(getrolepermissionlistBean);
			getrolepermissionlistCall.callService();			
			Logger.verifyEquals("4001", getrolepermissionlistCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}